//
//  JKRSharePlatformController.m
//  BaoJiDianJing
//
//  Created by Lucky on 2018/6/19.
//  Copyright © 2018年 KaiHei. All rights reserved.
//

#import "JKRSharePlatformController.h"
#import "JKRShareItemCell.h"
#import "MJExtension.h"
#import "JKRSharePresentationDelegate.h"
#import <YYKit.h>

@interface JKRSharePlatformController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) JKRSharePresentationDelegate *presentDelegate;
@property (weak, nonatomic) IBOutlet UIView *containerView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *collectionViewHeight;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic, strong) NSArray<JKRShareItem *> *shareItems;

@end

@implementation JKRSharePlatformController

static NSString *const cellID = @"JKRShareCellID";

+ (void)showShareMenuInCurrentController:(UIViewController *)currentController platformSelectionBlock:(UMSocialSharePlatformSelectionBlock)sharePlatformSelectionBlock {
    JKRSharePlatformController *controller = [[JKRSharePlatformController alloc] init];
    controller.shareBlock = sharePlatformSelectionBlock;
    if (!currentController) {
        currentController = [UIApplication sharedApplication].keyWindow.rootViewController;
    }
    [currentController presentViewController:controller animated:YES completion:nil];
}

- (instancetype)init {
    self = [self initWithNibName:@"JKRSharePlatformController" bundle:nil];
    
    return self;
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    self.transitioningDelegate = self.presentDelegate;
    self.modalPresentationStyle = UIModalPresentationCustom;
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat fw = [UIScreen mainScreen].bounds.size.width * 0.15;
    CGFloat ma = ([UIScreen mainScreen].bounds.size.width - fw * (self.shareItems.count >= 4 ? 4 : self.shareItems.count)) / (self.shareItems.count >= 4 ? 5.0f : self.shareItems.count + 1.f);
    CGFloat fh = fw * 1.67;
    flowLayout.itemSize = CGSizeMake(fw, fh);
    flowLayout.minimumLineSpacing = 5.f;
    flowLayout.minimumInteritemSpacing = ma;
    
    [self.collectionView setCollectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.contentInset = UIEdgeInsetsMake(15, ma, 15, ma);
    CGFloat line = ceilf(self.shareItems.count / 4.0);
    self.collectionViewHeight.constant = line * (fh + 5) + 27;
    
    if (([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125,2436), [[UIScreen mainScreen] currentMode].size) : NO)) {
        self.bottomHeight.constant = 70.f + 34.f;
    } else {
        self.bottomHeight.constant = 70.f;
    }
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"JKRShareItemCell" bundle:nil] forCellWithReuseIdentifier:cellID];
    
    [self.view layoutIfNeeded];
    
    self.containerView.top = kScreenHeight;
    self.view.userInteractionEnabled = NO;
    self.view.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.0];
    [UIView animateWithDuration:0.3 animations:^{
        self.view.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.62];
        self.containerView.top = kScreenHeight - self.containerView.height;
    } completion:^(BOOL finished) {
        self.view.userInteractionEnabled = YES;
    }];
}

- (IBAction)cancelButtonClick:(UIButton *)sender {
    self.view.userInteractionEnabled = NO;
    [UIView animateWithDuration:0.3 animations:^{
        self.view.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.0];
        self.containerView.top = kScreenHeight;
    } completion:^(BOOL finished) {
        self.view.userInteractionEnabled = YES;
        [self dismissViewControllerAnimated:NO completion:^{
            
        }];
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.shareItems.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    JKRShareItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    cell.item = self.shareItems[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    JKRShareItem *item = self.shareItems[indexPath.row];
    __weak typeof(self) weak_self = self;
    self.view.userInteractionEnabled = NO;
    [UIView animateWithDuration:0.3 animations:^{
        self.view.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.0];
        self.containerView.top = kScreenHeight;
    } completion:^(BOOL finished) {
        self.view.userInteractionEnabled = YES;
        [self dismissViewControllerAnimated:NO completion:^{
            if (weak_self.shareBlock) {
                weak_self.shareBlock(item.type.integerValue, nil);
            }
        }];
    }];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch = touches.anyObject;
    CGPoint point = [touch locationInView:self.view];
    if (!CGRectContainsPoint(self.containerView.frame, point)) {
        self.view.userInteractionEnabled = NO;
        [UIView animateWithDuration:0.3 animations:^{
            self.view.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.0];
            self.containerView.top = kScreenHeight;
        } completion:^(BOOL finished) {
            self.view.userInteractionEnabled = YES;
            [self dismissViewControllerAnimated:NO completion:nil];
        }];
    }
}

- (NSArray *)shareItems {
    if (!_shareItems) {
        NSMutableArray *items = [JKRShareItem mj_objectArrayWithFilename:@"JKRShareItems.plist"];
        NSMutableArray *result = [NSMutableArray array];
        for (JKRShareItem *item in items) {
            if ([[UMSocialManager defaultManager] isInstall:item.type.integerValue]) {
                [result addObject:item];
            }
        }
        _shareItems = [NSArray arrayWithArray:result];
    }
    return _shareItems;
}

- (JKRSharePresentationDelegate *)presentDelegate {
    if (!_presentDelegate) {
        _presentDelegate = [[JKRSharePresentationDelegate alloc] init];
    }
    return _presentDelegate;
}

- (void)dealloc {
    NSLog(@"分享弹窗销毁");
}

@end
